<template>
  <div>
      <h2>地址管理</h2>
      <van-address-list
  v-model="chosenAddressId"
  :list="list"
  default-tag-text="默认"
  @add="onAdd"
  @edit="onEdit"
  @select="select"
/>



  </div>
</template>
<script>
import { mapGetters, mapActions} from 'vuex';
import { Toast } from 'vant';
import {addressList} from "../../request/api"

export default {
  props: [],
  components: {},
  data() {
    return {
        userInfo:sessionStorage.getItem("token")?JSON.parse(sessionStorage.getItem("token")):{},

      chosenAddressId: '',
      list: [ ]
    };
},
  computed: {
    ...mapGetters({}),
},
  methods: {
    ...mapActions({}),
    onAdd() {
      // Toast('新增地址');
      this.$router.push("/addressAdd")
    },
    onEdit(item, index) {
      Toast('编辑地址:' + index);
    },
    select(item,index){
      console.log(item,index);
      this.$router.push("/order?id="+item.id)
      // this.$router.push({name:"order",query:item.id})
      Toast('切换地址成功');

    }
},
  mounted() {
    addressList({uid:this.userInfo.uid}).then(res=>{
      res.data.list.map((item)=>{
        this.list.push({
          id:item.id,
          name:item.username,
          tel:item.userphone,
          address:item.location,
          isDefault:item.status==1?true:false,
        })
      } )
        this.chosenAddressId= this.list.find(item=>{
            return item.isDefault
        }).id
    })
  },
};
</script>
<style scoped>
  .wrapper {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 100%;
  }
  .van-overlay{
  z-index: 999;

  }
</style>